/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package SYSGEST.br.UndSaude;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import sysgest.conexao.HibernateUtil;

/**
 *
 * @author Mesopotanea
 */
public class UndSaudeDAO {

    private Session sessao;
    private Transaction transacao;

    public void salvar(UndSaude undSau) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        sessao.save(undSau);
        transacao.commit();
        sessao.close();
    }

    public void alterar(UndSaude undS) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        sessao.update(undS);
        transacao.commit();
        sessao.close();
    }

    public List<UndSaude> listar() {
        sessao = HibernateUtil.getSessionFactory()
                .openSession();
        transacao = sessao.beginTransaction();
        Criteria criteria = sessao.createCriteria(UndSaude.class);
        List<UndSaude> undSaude = criteria.list();
        return undSaude;

    }
    
     public UndSaude pesquisacodigo(int codigo) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();

        UndSaude undS
                = (UndSaude) sessao.createCriteria(UndSaude.class).
                add(Restrictions.eq("codigo", codigo)).
                uniqueResult();
        sessao.close();
        return undS;

    }
     public void excluir(UndSaude undSaude) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        sessao.delete(undSaude);
        transacao.commit();
        sessao.close();
    }
      public List<UndSaude> pesquisarNome(String nome) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        List<UndSaude> unidade = sessao.createCriteria(UndSaude.class).
                add(Restrictions.like("unidade", nome, MatchMode.ANYWHERE))
                .addOrder(Order.asc("unidade")).list();
        return unidade;
    }
       public List<UndSaude> pesquisarCodigoESF(String codigo) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        List<UndSaude> unidade = sessao.createCriteria(UndSaude.class).
                add(Restrictions.like("codpsf", codigo, MatchMode.ANYWHERE))
                .addOrder(Order.asc("codpsf")).list();
        return unidade;
    }
         public UndSaude pesquisaESF(String Codigo) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        UndSaude saida
                = (UndSaude) sessao.createCriteria(UndSaude.class).add(Restrictions.eq("codpsf", Codigo)).uniqueResult();
        return saida;
    }
    
}
